package org.testis.orm.refinedclasses;

import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.classes.T_bed_dut;
import org.testis.orm.classes.T_bed_testpc;
import org.testis.orm.classes.T_test_stepcontainer;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author Cahit Emir Erel
 */
public class TestPlanRefined extends DesignPart {

    private T_test_stepcontainer m_reference;

    public TestPlanRefined(T_test_stepcontainer reference) {
	setReference(reference);
    }

    synchronized public TestPlanRefined createRefinedDatabaseCopy()
	    throws PersistentException {
	return new TestPlanRefined(createDatabaseCopy());
    }

    private T_test_stepcontainer createDatabaseCopy()
	    throws PersistentException {
	T_test_stepcontainer newContainer = new T_test_stepcontainer();
	newContainer.setContainerTypeId(getReference().getContainerTypeId());
	newContainer.setDescription(getReference().getDescription());
	newContainer.setName(getReference().getName());
	if (newContainer.save()) {
	    return newContainer;
	} else {
	    throw new PersistentException(
		    "Unable to create database copy of Test Plan: " + this);
	}
    }

    public String toString(TestDesignTreeNode treeNode) {
	try {
	    StringBuffer buffer = new StringBuffer();
	    buffer.append(this.toString());
	    buffer.append("\n\nBrowser Speed: "
		    + treeNode.getReference().getParameterStringValue(
			    TestDesignTreeParameters.BrowserSpeed.name()));
	    buffer.append("\nBrowser Type: "
		    + treeNode.getReference().getParameterStringValue(
			    TestDesignTreeParameters.BrowserType.name()));
	    Integer pcId = treeNode.getReference().getParameterIntValue(
		    TestDesignTreeParameters.PcId.name());
	    Integer deviceId = treeNode.getReference().getParameterIntValue(
		    TestDesignTreeParameters.DeviceId.name());
	    Integer nicId = treeNode.getReference().getParameterIntValue(
		    TestDesignTreeParameters.NicId.name());
	    String deviceIp = treeNode.getReference().getParameterStringValue(
		    TestDesignTreeParameters.DeviceIp.name());
	    if (deviceIp != null) {
		buffer.append("\nDevice Ip: " + deviceIp);
	    }
	    if (pcId != null) {
		buffer.append("\n\nConnected to\nPc Name: "
			+ TestDesign.getPcById(pcId).getName());
	    }
	    if (deviceId != null) {
		T_bed_dut dut = TestDesign.getDutById(deviceId);
		buffer.append("\n\nDevice Id: " + dut.getId());
		buffer.append("\nDevice Unique Name: " + dut.getName());
		buffer.append("\nDevice Test Bed Ip: " + dut.getTestBedIp());
		buffer.append("\nDevice Active Firmware: "
			+ dut.getActiveFirmware().getName());
	    } else if (nicId != null) {
		buffer.append("\n\nNic Id: "
			+ TestDesign.getNicById(nicId).getName());
	    }

	    return buffer.toString();
	} catch (NullPointerException ex) {
	    ex.printStackTrace();
	    return null;
	}
    }

    @Override
    public String toString() {
	try {
	    StringBuffer buffer = new StringBuffer();
	    buffer.append("Step Container Id:" + getId());
	    buffer.append("\nType: "
		    + TestDesign.getDesignPartTypeName(this.getReference()
			    .getContainerTypeId()));
	    buffer.append("\nName: " + getName());
	    buffer.append("\nDescription: " + getReference().getDescription());
	    return buffer.toString();
	} catch (NullPointerException ex) {
	    ex.printStackTrace();
	    return null;
	}
    }

    public T_test_stepcontainer getReference() {
	return m_reference;
    }

    private void setReference(T_test_stepcontainer reference) {
	if (reference == null) {
	    throw new NullPointerException();
	}
	m_reference = reference;
    }

    public String getTypeName() {
	return TestDesign.getDesignPartTypeName(this.getReference()
		.getContainerTypeId());
    }

    public int compare(Comparable source) {
	return Comparable.EXACT;
    }

    @Override
    public int compare(Comparable source, Comparable treeReference) {
	TestDesignTreeNode src = (TestDesignTreeNode) source;
	TestDesignTreeNode dest = (TestDesignTreeNode) treeReference;

	String srcBrowserSpeed = src.getReference().getParameterValue(
		TestDesignTreeParameters.BrowserSpeed.name());
	String srcBrowserType = src.getReference().getParameterValue(
		TestDesignTreeParameters.BrowserType.name());
	String srcPc = src.getReference().getParameterValue(
		TestDesignTreeParameters.PcId.name());
	String srcDevice = src.getReference().getParameterValue(
		TestDesignTreeParameters.DeviceId.name());
	String srcDeviceIp = src.getReference().getParameterValue(
		TestDesignTreeParameters.DeviceIp.name());
	String srcLang = src.getReference().getParameterValue(
		TestDesignTreeParameters.LanguageId.name());
	String srcProfile = src.getReference().getParameterValue(
		TestDesignTreeParameters.ProfileId.name());
	String srcTestBed = src.getReference().getParameterValue(
		TestDesignTreeParameters.TestBedId.name());

	String destBrowserSpeed = dest.getReference().getParameterValue(
		TestDesignTreeParameters.BrowserSpeed.name());
	String destBrowserType = dest.getReference().getParameterValue(
		TestDesignTreeParameters.BrowserType.name());
	String destPc = dest.getReference().getParameterValue(
		TestDesignTreeParameters.PcId.name());
	String destDevice = dest.getReference().getParameterValue(
		TestDesignTreeParameters.DeviceId.name());
	String destDeviceIp = dest.getReference().getParameterValue(
		TestDesignTreeParameters.DeviceIp.name());
	String destLang = src.getReference().getParameterValue(
		TestDesignTreeParameters.LanguageId.name());
	String destProfile = src.getReference().getParameterValue(
		TestDesignTreeParameters.ProfileId.name());
	String destTestBed = src.getReference().getParameterValue(
		TestDesignTreeParameters.TestBedId.name());

	if ((srcBrowserSpeed == null ? destBrowserSpeed != null
		: !srcBrowserSpeed.equals(destBrowserSpeed))
		|| (srcBrowserType == null ? destBrowserType != null
			: !srcBrowserType.equals(destBrowserType))
		|| (srcPc == null ? destPc != null : !srcPc.equals(destPc))
		|| (srcDevice == null ? destDevice != null : !srcDevice
			.equals(destDevice))
		|| (srcDeviceIp == null ? destDeviceIp != null : !srcDeviceIp
			.equals(destDeviceIp))
		|| (srcLang == null ? destLang != null : !srcLang
			.equals(destLang))
		|| (srcTestBed == null ? destTestBed != null : !srcTestBed
			.equals(destTestBed))
		|| (srcProfile == null ? destProfile != null : !srcProfile
			.equals(destProfile))) {
	    return Comparable.DIFFERENT_PROPERTIES;
	}
	return compare(src.getObject());
    }

    private boolean hasSameStaticParameters(HashMap<String, Object> params,
	    Comparable treeReference) {
	String description = (String) params.get("description");
	if (getReference().getDescription().equals(description)) {
	    return true;
	} else {
	    return false;
	}
    }

    public boolean compareParameters(HashMap<String, Object> params,
	    Comparable treeTreference) {

	return hasSameStaticParameters(params, treeTreference);
    }

    public boolean hasSameRunTimeParams(HashMap<String, Object> params,
	    Comparable reference) {
	String browserSpeed = (String) params.get("browserSpeed");
	String browserType = (String) params.get("browserType");
	String pc = (String) params.get("pc");
	String dut = (String) params.get("dut");
	Integer languageId = 0;
	if (params.containsKey("languageId")) {
	    languageId = Integer.parseInt((String) params.get("languageId"));
	}
	Integer testBedId = 0;
	if (params.containsKey("testBedId")) {
	    testBedId = Integer.parseInt((String) params.get("testBedId"));
	}
	Integer profileId = 0;
	if (params.containsKey("profileId")) {
	    profileId = Integer.parseInt((String) params.get("profileId"));
	}
	String deviceIp = (String) params.get("deviceIp");
	if (deviceIp == null) {
	    deviceIp = "";
	}
	TestDesignTreeNode node = (TestDesignTreeNode) reference;
	String currentBrowserSpeed = "";
	String currentBrowserType = "";
	String currentPc = "";
	String currentDut = "";
	Integer currentLanguageId = 0;
	Integer currentTestBedId = 0;
	Integer currentProfileId = 0;
	String currentDeviceIp = "";
	currentBrowserType = node.getReference().getParameterStringValue(
		TestDesignTreeParameters.BrowserType.name());
	currentBrowserSpeed = node.getReference().getParameterStringValue(
		TestDesignTreeParameters.BrowserSpeed.name());

	if (currentBrowserSpeed == null) {
	    currentBrowserSpeed = "600";
	}

	if (!currentBrowserSpeed.equals(browserSpeed)) {
	    return false;
	}
	if (currentBrowserType == null) {
	    currentBrowserType = "";
	}
	if (!currentBrowserType.equals(browserType) && !browserType.equals("*")) {
	    return false;
	}
	Integer pcId = node.getReference().getParameterIntValue(
		TestDesignTreeParameters.PcId.name());

	if (pcId != null) {
	    T_bed_testpc pcData = TestDesign.getPcById(pcId);
	    if (pcData != null) {
		currentPc = TestDesign.getPcById(pcId).getName();
	    }

	}
	if (!currentPc.equals(pc)) {
	    return false;
	}
	Integer deviceId = node.getReference().getParameterIntValue(
		TestDesignTreeParameters.DeviceId.name());
	if (deviceId != null) {
	    T_bed_dut dutData = TestDesign.getDutById(deviceId);
	    if (dutData != null) {
		currentDut = dutData.getName();
	    }

	}
	if (!currentDut.equals(dut)) {
	    return false;
	}
	currentDeviceIp = node.getReference().getParameterStringValue(
		TestDesignTreeParameters.DeviceIp.name());
	if (currentDeviceIp == null) {
	    currentDeviceIp = "";
	}
	if (!currentDeviceIp.equals(deviceIp)) {
	    return false;
	}
	currentTestBedId = node.getReference().getParameterIntValue(
		TestDesignTreeParameters.TestBedId.name());
	if (currentTestBedId == null) {
	    currentTestBedId = 0;
	}
	if (currentTestBedId.intValue() != testBedId.intValue()) {
	    return false;
	}
	currentProfileId = node.getReference().getParameterIntValue(
		TestDesignTreeParameters.ProfileId.name());
	if (currentProfileId == null) {
	    currentProfileId = 0;
	}
	if (currentProfileId.intValue() != profileId.intValue()) {
	    return false;
	}
	currentLanguageId = node.getReference().getParameterIntValue(
		TestDesignTreeParameters.LanguageId.name());
	if (currentLanguageId == null) {
	    currentLanguageId = 0;
	}
	if (currentLanguageId.intValue() != languageId.intValue()) {
	    return false;
	}
	return true;
    }
}
